<?php
/**
 * Created by PhpStorm.
 * User: liiyu
 * Date: 2018/6/7
 * Time: 1:51
 */

namespace app\api\controller\v14;

use app\api\controller\v2\Auth;
use app\api\controller\v4\CheckOrders;
use app\common\cachemodel\Orders as OrdersCacheModel;
use app\common\cachemodel\OrdersLib as OrdersCacheModelLib;
use app\common\cachemodel\RectificationEvent;
use app\common\cachemodel\ZhunleUserAccount;
use app\common\lib\exception\ApiException;
use app\common\lib\Orders as OrdersLib;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Log;
use think\response\Json;

/**
 * @version 1.9.8
 * 每日运势订单接口
 * Class Orders
 * @package app\api\controller\v2
 */
class Orders extends Auth
{
    /**
     * 接口，订单列表
     * @return Json
     * @throws Exception
     */
    public function index()
    {
        $this->msg = '';
        OrdersCacheModelLib::getUserAllUnpaidOrder($this->user['id']);
        $type = input('param.type/d', 0);
        $order_type = input('param.order_type/s', '');
        $version_code = input('param.version_code/d', 0);

        //1.9.6版本以后不需要过滤本命预测—
        if ($version_code)
        {
            switch (true)
            {
                case $this->headers['app-type'] === 'ios' && $version_code >= 22:
                case $this->headers['app-type'] === 'android' && $version_code >= 34:
                    $orders = (new OrdersCacheModel())->getUserAllOrder($this->user['id'], $type, $order_type);
                    break;
                default:
                    $orders = (new OrdersCacheModel())->getUserAllOrder($this->user['id'], $type, $order_type, '7');
                    break;
            }
        } else
        {
            $orders = (new OrdersCacheModel())->getUserAllOrder($this->user['id'], $type, $order_type, '7');
        }
        if ($orders)
        {
            $array = [];
            foreach ($orders['data'] as $k => $order_detail)
            {
                $order = $this->index_data_format($order_detail);
                if ($order)
                {
                    if ($order['trade_type'] == 13 || $order['trade_type'] == 8)
                    {
                        $order['trade_type'] = 2;
                    } elseif ($order['trade_type'] == 7)
                    {
                        $order['trade_type'] = 1;
                    }
                    $array[] = $order;
                }
            }
            $orders['data'] = $array;
        }
        return show(config('code.success'), '加载成功', $orders);
    }

    /**
     * 组合订单列表数据格式
     * @param $order_detail
     * @return array | string
     * @throws Exception
     */
    private function index_data_format($order_detail)
    {
        try
        {
            $order = OrdersCacheModelLib::getOrderDetails($order_detail);
            if ($order === false)
            {
                return $this->msg = '订单数据错乱';
            }
            // 处理苹果审核时价格
            $order_detail['order_type'] = (int)$order_detail['order_type'];
            if ($this->is_apple_pay && $order_detail['order_type'] !== 8)
            {
                switch ($order_detail['order_type'])
                {
                    case 1:
                        $apple_goods = $this->apple_goods['rec_goods'];
                        break;
                    case 2:
                        $apple_goods = $this->apple_goods['fortune_day_goods'];
                        break;
                    case 3:
                        $apple_goods = $this->apple_goods['fortune_month_goods'];
                        break;
                    case 4:
                        $apple_goods = $this->apple_goods['fortune_year_goods'];
                        break;
                    case  5:
                        $apple_goods = $this->apple_goods['acg_goods'];
                        break;
                    case 6:
                        $apple_goods = $this->apple_goods['video_goods'];
                        break;
                    case 7:
                        $apple_goods = $this->apple_goods['natal_report_goods'];
                        break;
//                case 8:
//                    $apple_goods = $this->apple_goods['vip_goods'];
//                    break;
                    default:
                        $apple_goods = [];
                        break;
                }
                $total_amount = 0;
                foreach ($apple_goods as $ag)
                {
                    foreach ($order['goods_data'] as $gid => $odg)
                    {
                        if ((int)$ag['id'] === (int)$odg['id'])
                        {
                            $total_amount += $ag['now_prize'];
                            $order['goods_data'][$gid] = array_merge($odg, $ag);
                        }
                    }
                }
                $order_detail['total_amount'] = $total_amount;
            }
            $data = [];
            $data['data'] = [];
            $order_data = [];
            switch ($order_detail['env_type'])
            {
                case 2:
                    $orderTradeNmae = '（微信）';
                    break;
                case 3:
                    $orderTradeNmae = '（抖音）';
                    break;
                case 4:
                    $orderTradeNmae = '（支付宝）';
                    break;
                case 5:
                    $orderTradeNmae = '（百度）';
                    break;
                case 6:
                    $orderTradeNmae = '（QQ）';
                    break;
                case 7:
                    $orderTradeNmae = '（h5）';
                    break;
                default:
                    $orderTradeNmae = '';
                    break;
            }
            switch (true)
            {
                //重组校准生辰订单
                case (int)$order_detail['order_type'] === 1:
                    $data['order_name'] = '校准生辰' . $orderTradeNmae;
                    $order_data['title'] = '校准生辰服务';
                    $order_data['des1'] = '出生时间：' . date('Y-m-d', $order['rectification_data']['start_time']);
                    $order_data['des2'] = '时间范围：' . date('H:i', $order['rectification_data']['start_time']) . '-' . ((date('d', $order['rectification_data']['start_time']) != date('d', $order['rectification_data']['end_time'])) ? '次日' : '') . date('H:i', $order['rectification_data']['end_time']);
                    $data['data'][] = $order_data;
                    break;
                //重组运势订单
                case in_array((int)$order_detail['order_type'], [2, 3, 4], true):
                    switch ($order_detail['order_type'])
                    {
                        case 2:
                            $order_data['title'] = '每日运势';
                            break;
                        case 3:
                            $order_data['title'] = '月度运势';
                            break;
                        case 4:
                            $order_data['title'] = '年度运势';
                            break;
                        default :
                            $order_data['title'] = '';
                            break;
                    }
                    $data['order_name'] = '运势订单' . $orderTradeNmae;
                    $frame = [];
                    foreach ($order['recode_data'] as $for)
                    {
                        $frame[] = $for['name'];
                        $order_data['des2'] = '有效日期：' . $order['fortune_data']['start_time'] . '至' . $order['fortune_data']['end_time'];
                    }
                    $frame = $frame ? implode(',', $frame) : '';
                    $order_data['des1'] = '开通档案：' . $frame;
                    $data['data'][] = $order_data;
                    break;
                //重组地图订单
                case (int)$order_detail['order_type'] === 5:
                    $data['order_name'] = '幸运之城' . $orderTradeNmae;
                    $order_data['title'] = '幸运之城解锁';
                    $map_recode = $order['recode_data'] ? current($order['recode_data']) : [];
                    $game = [];
                    foreach ($order['goods_data'] as $acg)
                    {
                        $game[] = $acg['name'];
                    }
                    $map_count = count($game);
                    $gabe_sta = $game ? implode(',', $game) : '';
                    if ($map_count > 2)
                    {
                        $des = '开通' . implode(',', array_slice($game, 0, 2)) . '等' . $map_count . '条';
                    } else
                    {
                        $des = '开通' . $gabe_sta . '等' . $map_count . '条';
                    }
                    $order_data['des1'] = '购买档案：' . ($map_recode['name'] ?? '匿名用户');
                    $order_data['des2'] = '订单详情：' . $des;
                    $data['order_data'][] = [
                        'name' => '订单详情',
                        'value' => $gabe_sta
                    ];
                    $data['data'][] = $order_data;
                    break;
                //重组本命预测订单
                case (int)$order_detail['order_type'] === 7:
                    $data['order_name'] = '一生报告订单' . $orderTradeNmae;
                    $order_data['title'] = '一生报告解锁';
                    $game = [];
                    foreach ($order['goods_data'] as $acg)
                    {
                        $game[] = $acg['name'];
                    }
                    $map_count = count($game);
                    $gabe_sta = $game ? implode(',', $game) : '';
                    if ($map_count > 1)
                    {
                        $des = '开通' . implode(',', array_slice($game, 0, 1)) . '等' . $map_count . '篇';
                    } else
                    {
                        $des = '开通' . $gabe_sta;
                    }
                    $order_data['des1'] = $des;
                    $data['order_data'][] = [
                        'name' => '订单详情',
                        'value' => $gabe_sta
                    ];
                    $data['data'][] = $order_data;
                    break;
                //重组视频订单
                case (int)$order_detail['order_type'] === 6:
                    $video_goods = $order['goods_data'] ? current($order['goods_data']) : [];
                    $data['order_name'] = '教学视频' . $orderTradeNmae;
                    $data['video_id'] = $order['video_id'];
                    $order_data['title'] = $video_goods['name'];
                    $order_data['des1'] = $order['goods_data_des'];
                    $data['data'][] = $order_data;
                    break;
                //重组vip订单
                case (int)$order_detail['order_type'] === 8:
                    $vip_goods = $order['goods_data'] ? current($order['goods_data']) : [];
//                $vip_goods_des = json_decode($vip_goods['extend'], true)['goods_des'] ?? '';
                    $data['order_name'] = '开通会员' . $orderTradeNmae;
                    $order_data['title'] = '会员开通';
                    $order_data['des1'] = '开通时长：' . $vip_goods['name'];
                    //若会员购买成功后，显示有效期
                    if ((int)$order['status'] === 4)
                    {
                        $order_data['des2'] = '有效期至：' . $order['vip_end_time'];
                    }
                    $data['data'][] = $order_data;
                    break;
            }
            $data['order_type'] = (int)$order_detail['order_type'];
            $data['order_status'] = $order['status_des'];
            $data['order_id'] = $order_detail['order_id'];
            $data['create_time'] = date('Y-m-d H:i:s', $order_detail['create_time']);
            $data['total_amount'] = $order_detail['total_amount'];
            $data['trade_type'] = (int)$order_detail['trade_type'];
            //获取订单按钮数据,列表没有取消订单
            if (isset($order['order_btn'][4]))
            {
                unset($order['order_btn'][4]);
            }
            $data['order_btns'] = $this->getIndexOrderBtn($order_detail);
            return $data;
        }
        catch (Exception $e)
        {
            Log::write($this->user['id'] . '订单列表错误：' . $e->getMessage() . '【' . $e->getFile() . $e->getLine() . '】', 'error', true);
        }
        return [];
    }

    /**
     * 获取订单列表按钮
     * @param $orders
     * @return mixed
     */
    private function getIndexOrderBtn($orders)
    {
        // 如果是苹果支付，并且是未支付的情况，去支付在按钮不显示
        if ($orders['trade_type'] == 9 && $orders['status'] == 0)
        {
            return [];
        }
        $bans = config('zhunleorder');
        $data = $bans['index_order_btn'][$orders['status']];
        $array = [];
        if (in_array((int)$orders['status'], [3, 4], true) && in_array((int)$orders['order_type'], [1, 5, 6], true))
        {
            $array[] = array_shift($data);
        } else
        {
            $array = $data;
        }
        return $array;
    }

    /**
     * 接口，获取订单详情
     * @throws Exception
     */
    public function read()
    {
        $this->msg = '';
        $param = $this->request->only(['order_id', 'order_type']);
        if (!isset($param['order_id']) || !$param['order_id'])
        {
            return show(config('code.error'), '未知的订单');
        }
        $ordersCacheModel = new OrdersCacheModel();
        $orders = current($ordersCacheModel->batchGetInfo([$param['order_id']]));
        if ($orders)
        {
            if ($orders['uid'] != $this->user['id'])
            {
                return show(config('code.error'), '服务异常，请稍后重试～');
            }
            $info = $this->read_data_format($orders);
            return show(config('code.success'), '获取成功', $info);
        }
        return show(config('code.error'), '订单不存在');
    }

    /**
     * 组合订单详情数据格式
     * @param $orders
     * @return array
     * @throws Exception
     */
    private function read_data_format($orders)
    {
        try
        {
            $skuId = '';
            $orders_details = OrdersCacheModelLib::getOrderDetails($orders);
            if ((int)$orders_details['status'] === 2)
            {
                //关闭超时订单
                $orders['status'] = 2;
            }
            //订单数据重组
            $data['order_id'] = $orders['order_id'];
            $info = [
                'status_des' => $orders_details['status_des'],
                'order_icon' => $this->getReadOrderBtn($orders)['icon'],
                'total_amount' => $orders_details['total_amount'],
                'trade_type' => (int)$orders['trade_type'],
                'order_id' => $orders['order_id'],
                'order_data' => $orders_details['order_data'],
                'is_skip' => $orders_details['is_skip'],
                'order_btn' => $this->getReadOrderBtn($orders)['btn'],
                'is_display_recommend_data' => $orders_details['is_display_recommend_data'],
            ];
            if ($orders_details['mini_type_des'])
            {
                $info['status_des'] .= $orders_details['mini_type_des'];
            }
            //订单描述，若为等待支付和进行中时有此字段
            if (isset($orders_details['des']))
            {
                $info['des'] = $orders_details['des'];
            }
            //剩余时间，以秒为单位（半小时为起始值） ， 若为等待支付时此字段有
            if (isset($orders_details['time_remaining']))
            {
                $info['time_remaining'] = $orders_details['time_remaining'];
            }
            //购买时的优惠券信息，无此字段不显示
            if (isset($orders_details['coupon_data']))
            {
                $info['coupon_data'] = $orders_details['coupon_data'];
            }
            //购买时的折扣信息，无此字段不显示
            if (isset($orders_details['active_data']))
            {
                $info['active_data'] = $orders_details['active_data'];
            }
            //重组购买的商品信息
            if (in_array((int)$orders['order_type'], [1, 2, 3, 4, 5, 6, 7, 8], true))
            {
                $goods = $orders_details['goods_data'] ? current($orders_details['goods_data']) : [];
                $info['goods_data']['price'] = (string)$goods['now_prize'];
                $info['goods_data']['des'] = $orders_details['goods_data_des'] ?? '';

                switch (true)
                {
                    case (int)$orders['order_type'] === 1:
                        $info['goods_data']['name'] = '校准生辰服务';
                        break;
                    case (int)$orders['order_type'] === 2:
                        $info['goods_data']['name'] = '每日运势开通';
                        break;
                    case (int)$orders['order_type'] === 3:
                        $info['goods_data']['name'] = '月度运势开通';
                        break;
                    case (int)$orders['order_type'] === 4:
                        $info['goods_data']['name'] = '年度运势开通';
                        break;
                    case (int)$orders['order_type'] === 5:
                        $info['goods_data']['name'] = '幸运之城解锁';
                        // 处理幸运之城多订单购买时，价格错误问题
                        try
                        {
                            $info['goods_data']['price'] = (string)array_sum(array_column($orders_details['goods_data'], 'now_prize'));
                        }
                        catch (Exception $e)
                        {
                            $info['goods_data']['price'] = '0';
                        }
                        break;
                    case (int)$orders['order_type'] === 7:
                        $info['goods_data']['name'] = '一生报告解锁';
                        try
                        {
                            $info['goods_data']['price'] = (string)array_sum(array_column($orders_details['goods_data'], 'now_prize'));
                        }
                        catch (Exception $e)
                        {
                            $info['goods_data']['price'] = '0';
                        }
                        break;
                    case (int)$orders['order_type'] === 8:
                        $info['goods_data']['name'] = '开通会员';
                        $info['goods_data']['des'] = $goods['name'];
                        //若会员支付成功，则显示有效期
                        if ((int)$orders_details['status'] === 4)
                        {
                            $info['goods_data']['start_time'] = $orders_details['vip_start_time'] ?? '';
                            $info['goods_data']['end_time'] = $orders_details['vip_end_time'] ?? '';
                        }
                        break;
                    default:
                        $info['goods_data']['name'] = $goods['name'];
                        break;
                }

                //重组运势信息
                if (in_array((int)$orders['order_type'], [2, 3, 4], true))
                {
                    $info['goods_data']['start_time'] = $orders_details['fortune_data']['start_time'];
                    $info['goods_data']['end_time'] = $orders_details['fortune_data']['end_time'];
                }
                //重组视频信息
                if ((int)$orders['order_type'] === 6)
                {
                    $info['goods_data']['id'] = $orders_details['video_id'] ?? 0;
                }
            } else
            {
                $info['goods_data']['price'] = (string)$orders_details['goods_data_price'];
                $info['goods_data']['des'] = $orders_details['goods_data_des'];
                $info['goods_data']['name'] = $orders_details['goods_name'] ?? $orders_details['goods_data_des'];
            }
            //重组购买的档案，无此字段不显示
            $recode_num = $orders_details['recode_data'] ? count($orders_details['recode_data']) : 0;
            $recode_rids = [];
            if ($recode_num)
            {
                if ($recode_num > 1)
                {
                    $info['recode_data']['des'] = $recode_num . '份档案';
                    foreach ($orders_details['recode_data'] as $recode_datum)
                    {
                        $recode_rids[] = $recode_datum['id'];
                    }
                } else
                {
                    $recode = current($orders_details['recode_data']);
                    $info['recode_data']['des'] = $recode['name'];
                    $recode_rids[] = $recode['id'];
                }
            }
            if ($recode_rids)
            {
                $info['recode_data']['rids'] = implode(',', $recode_rids);
            }

            //重组校准生辰事件信息，无此字段不显示
            if ((int)$orders['order_type'] === 1)
            {
                $info['goods_data']['id'] = $orders['reid'];
                $info['rectification_data']['des1'] = date('Y-m-d', $orders_details['rectification_data']['start_time']);
                $info['rectification_data']['des2'] = date('H:i', $orders_details['rectification_data']['start_time']) . '至' . ((date('d', $orders_details['rectification_data']['start_time']) != date('d', $orders_details['rectification_data']['end_time'])) ? '次日' : '') . date('H:i', $orders_details['rectification_data']['end_time']);
                //获取校准生辰事件信息
                $events = (new RectificationEvent())->batchGetInfoByIds($orders['reid'], ['id', 'status', 'reid', 'time', 'type', 'desc']);
                foreach ($events as $k => $event)
                {
                    unset($event['status'], $event['reid']);
                    $event['time'] = $event['time'] ? date('Y-m', $event['time']) : '';
                    $events[$k] = $event;
                }
                $info['rectification_data']['event'] = $events;
            }
            $info['coin_num'] = OrdersCacheModelLib::getCoinNum($this->user['id']);
            $info['skuId'] = $skuId;
            return $info;
        }
        catch (Exception $e)
        {
            Log::write($orders['order_id'] . '订单详情错误：' . $e->getMessage() . '【' . $e->getFile() . $e->getLine() . '】', 'error', true);
            return $orders;
        }
    }

    /**
     * 获取订单详情按钮和图标
     * @param $orders
     * @return mixed
     */
    private function getReadOrderBtn($orders)
    {
        $bans = config('zhunleorder');
        $data['icon'] = $bans['order_icon'][$orders['status']];
        $data['btn'] = $bans['read_order_btn'][$orders['status']];
        if (in_array((int)$orders['status'], [3, 4], true) && in_array((int)$orders['order_type'], [1, 5, 6], true))
        {
            $data['btn'] = [];
        }
        return $data;
    }

    /**
     * 订单重新支付
     * @return Json
     * @throws ApiException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws Exception
     */
    public function pay_again()
    {
        if (!request()->isPost())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $param['order_id'] = input('param.order_id', '');
        $param['trade_type'] = input('param.trade_type/d', 0);
        $param['openid'] = input('param.openid', '');
        $callback_url = $param['callback_url'] ?? '';
        $param['password'] = input('param.password/s', '');

        if (!$param['order_id'])
        {
            return show(config('code.error'), '未知的订单');
        }
        if (!$param['trade_type'])
        {
            return show(config('code.error'), '未知的支付方式');
        }

        $OrdersLib = new OrdersLib();
        $CheckOrders = new CheckOrders();
        $ordersModel = new OrdersCacheModel();
        //检测当前订单是否存在
        $orders = $ordersModel->getInfoById($param['order_id']);
        if (!$orders || $orders['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '当前订单不存在');
        }

        //0=>未支付  1=>等待计算|等待矫正   2=>交易关闭   3=>退款成功  4=>计算成功|矫正成功|已支付
        if ($orders['status'])
        {
            return show(config('code.error'), '当前订单不可支付，请重新确认');
        }

        //支付客户端判断该订单是否已经支付,若已经支付,但是status=0;就更改status=1
        $validateResult = $OrdersLib->validateOrders($param['order_id']);
        if ($validateResult !== true)
        {
            return show(config('code.error'), $validateResult);
        }

        switch (true)
        {
            //获取校准生辰订单
            case (int)$orders['order_type'] === 1:
                $rec = $ordersModel->RecRead($orders);
                $order_rid = array_column($rec['recode_data'], 'id');
                break;
            //获取运势订单
            case in_array((int)$orders['order_type'], [2, 3, 4, 12], true):
                $fortune = $ordersModel->FortuneRead($orders);
                $order_rid = array_column($fortune['recode_data'], 'id');
                break;
            //获取地图订单
            case (int)$orders['order_type'] === 5:
                $map = $ordersModel->MapRead($orders);
                $order_rid = array_column($map['recode_data'], 'id');
                break;
            //获取本命预测订单
            case (int)$orders['order_type'] === 7:
                $report = $ordersModel->MapRead($orders);
                $order_rid = array_column($report['recode_data'], 'id');
                break;
            default:
                $order_rid = [0];
                break;
        }
        $orders['rid'] = current($order_rid);

        //判断购买的档案的订单是否被删除并获取解锁的商品
        if ($orders['order_type'] == 14)
        {
            $unlock_gids = [$orders['gid']];
            //获取订单类型和支付描述
            $order_des_info = OrdersCacheModelLib::createMallOrderId($orders['gid']);
        } else
        {
            // 检查商品是否已经购买
            $CheckOrders->check_old_orders($param['order_id']);
            $unlock_gids = $orders['gids'] = $this->payOrder($orders);
            //获取订单类型和支付描述语
            $order_des_info = OrdersCacheModelLib::createOrderId($orders['order_type'], $orders['gid']);
        }

        //组合苹果商品id
        $apple_gid = [];
        foreach ($unlock_gids as $gid)
        {
            $apple_gid[] = 'apple_pay_' . $gid;
        }

        /**
         * 下单支付
         */
        if ($param['trade_type'] === 4)//准币支付
        {
            //检测准币是否充足
            $userCoinInfo = (new ZhunleUserAccount())->getUserCoinFromCache($this->user['id']);
            $param['coin_num'] = OrdersCacheModelLib::getOrderCoinNum($orders['total_amount']);
            (new ZhunleUserAccount())->verifyCoinPay($userCoinInfo, $param['password'], $param['coin_num']);
            $data['data']['order_id'] = $param['order_id'];
            $res = (new \app\common\service\Orders())->notify_handle($param['order_id'], $orders['total_amount'], 4);
            if ($res === false)
            {
                return show(config('code.error'), '服务异常，请稍后再试');
            }
            //准币余额修改，添加明细记录
            OrdersCacheModelLib::UpdateCoin($orders['order_id'], $orders['order_type'], $orders['total_amount'], $this->user['id'], 2, 4);
            $distinctId = $this->request->header('distinct-id', '');
            if ($distinctId)
            {
                #神策打点 准币支付成功
                (new \app\common\service\Orders())->getSensorsTrackData($orders['order_id'], 'buy_success');
            }
        } else//三方支付
        {
            /**
             * 更新支付方式 和 当前使用的支付配置
             */
            try
            {
                (new OrdersCacheModel())->UpdateData($param['order_id'], ['trade_type' => $param['trade_type'], 'account' => $orders['account']]);
                //支付订单
                $data = OrdersCacheModelLib::unifyPayOrder([
                    'oid' => $orders['id'],
                    'trade_type' => $param['trade_type'],
                    'total_amount' => $orders['total_amount'],
                    'pay_des' => $order_des_info['pay_des'],
                    'order_id' => $param['order_id'],
                    'openid' => '',
                    'callback_url' => $callback_url,
                    'order_type' => $orders['order_type']
                ], 1);
                $data['data']['skuId'] = implode(',', $apple_gid);
            }
            catch (Exception $e)
            {
                Log::write($param['order_id'] . '重新支付更新订单状态失败' . $e->getMessage() . '【' . $e->getFile() . $e->getLine() . '】');
            }
        }
        return show(config('code.success'), '下单成功', $data['data']);
    }

    /**
     * @param $orders
     * @return array
     * @throws ApiException
     */
    private function payOrder($orders)
    {
        $ordersModel = new OrdersCacheModel();
        $recode = [];
        $gids = [];
        switch (true)
        {
            //重组校准生辰订单
            case (int)$orders['order_type'] === 1:
                $rec = $ordersModel->RecRead($orders);
                $recode = $rec['recode_data'];
                $gids[] = $orders['gid'];
                break;
            //重组运势订单
            case in_array((int)$orders['order_type'], [2, 3, 4, 12], true):
                $fortune = $ordersModel->FortuneRead($orders);
                $recode = $fortune['recode_data'];
                $gids[] = $orders['gid'];
                break;
            //重组地图订单
            case (int)$orders['order_type'] === 5:
                $map = $ordersModel->MapRead($orders);
                $recode = $map['recode_data'];
                $gids = array_column($map['goods_data'], 'id');
                break;
            //重组本命预测订单
            case (int)$orders['order_type'] === 7:
                $report = $ordersModel->MapRead($orders);
                $recode = $report['recode_data'];
                $gids = array_column($report['goods_data'], 'id');
                break;
            //重组视频订单
            case (int)$orders['order_type'] === 6:
//                $video = $ordersModel->VideoRead($orders);
                $gids[] = $orders['gid'];
                break;
            //重组vip订单 和 合盘报告订单
            case (int)$orders['order_type'] === 8:
            case (int)$orders['order_type'] === 17:
//                $vip = $ordersModel->VipRead($orders);
                $gids[] = $orders['gid'];
                break;
        }
        foreach ($recode as $re)
        {
            if (!$re['status'])
            {
                throw new ApiException('该订单有档案已删除，无法支付');
            }
        }
        return $gids;
    }
}
